home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gdevokii.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  8.8 KB  |  328 lines

  1. /* Copyright (C) 1989, 1992, 1995 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gdevokii.c,v 1.2 2000/09/19 19:00:14 lpd Exp $*/
  20. /*
  21.  * Okidata IBM compatible dot-matrix printer driver for Ghostscript.
  22.  *
  23.  * This device is for the Okidata Microline IBM compatible 9 pin dot
  24.  * matrix printers.  It is derived from the Epson 9 pin printer driver
  25.  * using the standard 1/72" vertical pin spacing and the 60/120/240
  26.  * dpi horizontal resolutions.  The vertical feed resolution however
  27.  * is 1/144" and the Okidata implements the standard 1/216" requests
  28.  * through "scaling":
  29.  *
  30.  *   (power on)
  31.  *   "\033J\001" (vertical feed 1/216")  => Nothing happens
  32.  *   "\033J\001" (vertical feed 1/216")  => Advance 1/144"
  33.  *   "\033J\001" (vertical feed 1/216")  => Advance 1/144"
  34.  *   "\033J\001" (vertical feed 1/216")  => Nothing happens
  35.  *   (and so on)
  36.  *
  37.  * The simple minded accounting used here keep track of when the
  38.  * page actually advances assumes the printer starts in a "power on"
  39.  * state.
  40.  * 
  41.  * Supported resolutions are:
  42.  *
  43.  *    60x72      60x144
  44.  *   120x72     120x144
  45.  *   240x72     240x144
  46.  *
  47.  */
  48. #include "gdevprn.h"
  49.  
  50. /*
  51.  * Valid values for X_DPI:
  52.  *
  53.  *     60, 120, 240
  54.  *
  55.  * The value specified at compile time is the default value used if the
  56.  * user does not specify a resolution at runtime.
  57.  */
  58.  
  59. #ifndef X_DPI
  60. #  define X_DPI 120
  61. #endif
  62.  
  63. /*
  64.  * Valid values for Y_DPI:
  65.  *
  66.  *     72, 144
  67.  *
  68.  * The value specified at compile time is the default value used if the
  69.  * user does not specify a resolution at runtime.
  70.  */
  71.  
  72. #ifndef Y_DPI
  73. #  define Y_DPI 72
  74. #endif
  75.  
  76. /* The device descriptor */
  77. private dev_proc_print_page(okiibm_print_page);
  78.  
  79. /* Okidata IBM device */
  80. gx_device_printer far_data gs_okiibm_device =
  81.   prn_device(prn_std_procs, "okiibm",
  82.     DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  83.     X_DPI, Y_DPI,
  84.     0.25, 0.0, 0.25, 0.0,            /* margins */
  85.     1, okiibm_print_page);
  86.  
  87. /* ------ Internal routines ------ */
  88.  
  89. /* Forward references */
  90. private void okiibm_output_run(P6(byte *, int, int, char, FILE *, int));
  91.  
  92. /* Send the page to the printer. */
  93. private int
  94. okiibm_print_page1(gx_device_printer *pdev, FILE *prn_stream, int y_9pin_high,
  95.   const char *init_string, int init_length,
  96.   const char *end_string, int end_length)
  97. {    
  98.     static const char graphics_modes_9[5] =
  99.     {    
  100.     -1, 0 /*60*/, 1 /*120*/, -1, 3 /*240*/
  101.     };
  102.  
  103.     int in_y_mult = (y_9pin_high ? 2 : 1);
  104.     int line_size = gdev_mem_bytes_per_scan_line((gx_device *)pdev);
  105.     /* Note that in_size is a multiple of 8. */
  106.     int in_size = line_size * (8 * in_y_mult);
  107.     byte *buf1 = (byte *)gs_malloc(in_size, 1, "okiibm_print_page(buf1)");
  108.     byte *buf2 = (byte *)gs_malloc(in_size, 1, "okiibm_print_page(buf2)");
  109.     byte *in = buf1;
  110.     byte *out = buf2;
  111.     int out_y_mult = 1;
  112.     int x_dpi = pdev->x_pixels_per_inch;
  113.     char start_graphics = graphics_modes_9[x_dpi / 60];
  114.     int first_pass = (start_graphics == 3 ? 1 : 0);
  115.     int last_pass = first_pass * 2;
  116.     int y_passes = (y_9pin_high ? 2 : 1);
  117.     int skip = 0, lnum = 0, pass, ypass;
  118.     int y_step = 0;
  119.  
  120.     /* Check allocations */
  121.     if ( buf1 == 0 || buf2 == 0 )
  122.     {    if ( buf1 ) 
  123.           gs_free((char *)buf1, in_size, 1, "okiibm_print_page(buf1)");
  124.         if ( buf2 ) 
  125.           gs_free((char *)buf2, in_size, 1, "okiibm_print_page(buf2)");
  126.         return_error(gs_error_VMerror);
  127.     }
  128.  
  129.     /* Initialize the printer. */
  130.     fwrite(init_string, 1, init_length, prn_stream);
  131.  
  132.     /* Print lines of graphics */
  133.     while ( lnum < pdev->height )
  134.     {    
  135.         byte *in_data;
  136.         byte *inp;
  137.         byte *in_end;
  138.         byte *out_end;
  139.         int lcnt;
  140.  
  141.         /* Copy 1 scan line and test for all zero. */
  142.         gdev_prn_get_bits(pdev, lnum, in, &in_data);
  143.         if ( in_data[0] == 0 &&
  144.              !memcmp((char *)in_data, (char *)in_data + 1, line_size - 1)
  145.            )
  146.             {    
  147.             lnum++;
  148.             skip += 2 / in_y_mult;
  149.             continue;
  150.         }
  151.  
  152.         /*
  153.          * Vertical tab to the appropriate position.
  154.          * The skip count is in 1/144" steps.  If total
  155.          * vertical request is not a multiple od 1/72"
  156.          * we need to make sure the page is actually
  157.          * going to advance.
  158.          */
  159.         if ( skip & 1 )
  160.         {
  161.             int n = 1 + (y_step == 0 ? 1 : 0);
  162.             fprintf(prn_stream, "\033J%c", n);
  163.             y_step = (y_step + n) % 3;
  164.             skip -= 1;
  165.         }
  166.         skip = skip / 2 * 3;
  167.         while ( skip > 255 )
  168.         {    
  169.             fputs("\033J\377", prn_stream);
  170.             skip -= 255;
  171.         }
  172.         if ( skip )
  173.         {
  174.             fprintf(prn_stream, "\033J%c", skip);
  175.         }
  176.  
  177.         /* Copy the the scan lines. */
  178.             lcnt = gdev_prn_copy_scan_lines(pdev, lnum, in, in_size);
  179.         if ( lcnt < 8 * in_y_mult )
  180.         {    /* Pad with lines of zeros. */
  181.             memset(in + lcnt * line_size, 0,
  182.                    in_size - lcnt * line_size);
  183.         }
  184.  
  185.         if ( y_9pin_high )
  186.         {    /* Shuffle the scan lines */
  187.             byte *p;
  188.             int i;
  189.             static const char index[] =
  190.             {  0, 2, 4, 6, 8, 10, 12, 14,
  191.                1, 3, 5, 7, 9, 11, 13, 15
  192.             };
  193.             for ( i = 0; i < 16; i++ )
  194.             {
  195.                 memcpy( out + (i * line_size),
  196.                         in + (index[i] * line_size),
  197.                         line_size);
  198.             }
  199.             p = in;
  200.             in = out;
  201.             out = p;
  202.         }
  203.  
  204.     for ( ypass = 0; ypass < y_passes; ypass++ )
  205.     {
  206.         for ( pass = first_pass; pass <= last_pass; pass++ )
  207.         {
  208.         /* We have to 'transpose' blocks of 8 pixels x 8 lines, */
  209.         /* because that's how the printer wants the data. */
  210.  
  211.             if ( pass == first_pass )
  212.             {
  213.             out_end = out;
  214.             inp = in;
  215.             in_end = inp + line_size;
  216.     
  217.                     for ( ; inp < in_end; inp++, out_end += 8 )
  218.                     { 
  219.                     gdev_prn_transpose_8x8(inp + (ypass * 8 * line_size), 
  220.                            line_size, out_end, 1);
  221.             }
  222.             /* Remove trailing 0s. */
  223.             while ( out_end > out && out_end[-1] == 0 )
  224.                 {
  225.                    out_end--;
  226.             }
  227.         }
  228.  
  229.         /* Transfer whatever is left and print. */
  230.         if ( out_end > out )
  231.             {
  232.             okiibm_output_run(out, (int)(out_end - out),
  233.                        out_y_mult, start_graphics,
  234.                    prn_stream, pass);
  235.             }
  236.             fputc('\r', prn_stream);
  237.         }
  238.         if ( ypass < y_passes - 1 )
  239.         {
  240.         int n = 1 + (y_step == 0 ? 1 : 0);
  241.         fprintf(prn_stream, "\033J%c", n);
  242.         y_step = (y_step + n) % 3;
  243.         }
  244.     }
  245.     skip = 16 - y_passes + 1;        /* no skip on last Y pass */
  246.     lnum += 8 * in_y_mult;
  247.     }
  248.  
  249.     /* Reinitialize the printer. */
  250.     fwrite(end_string, 1, end_length, prn_stream);
  251.     fflush(prn_stream);
  252.  
  253.     gs_free((char *)buf2, in_size, 1, "okiibm_print_page(buf2)");
  254.     gs_free((char *)buf1, in_size, 1, "okiibm_print_page(buf1)");
  255.     return 0;
  256. }
  257.  
  258. /* Output a single graphics command. */
  259. /* pass=0 for all columns, 1 for even columns, 2 for odd columns. */
  260. private void
  261. okiibm_output_run(byte *data, int count, int y_mult,
  262.   char start_graphics, FILE *prn_stream, int pass)
  263. {    
  264.     int xcount = count / y_mult;
  265.  
  266.     fputc(033, prn_stream);
  267.     fputc("KLYZ"[start_graphics], prn_stream);
  268.     fputc(xcount & 0xff, prn_stream);
  269.     fputc(xcount >> 8, prn_stream);
  270.     if ( !pass )
  271.     {
  272.         fwrite(data, 1, count, prn_stream);
  273.     }
  274.     else
  275.     {    
  276.         /* Only write every other column of y_mult bytes. */
  277.         int which = pass;
  278.         register byte *dp = data;
  279.         register int i, j;
  280.  
  281.         for ( i = 0; i < xcount; i++, which++ )
  282.         {
  283.             for ( j = 0; j < y_mult; j++, dp++ )
  284.             {
  285.                 putc(((which & 1) ? *dp : 0), prn_stream);
  286.             }
  287.         }
  288.     }
  289. }
  290.  
  291. /* The print_page procedures are here, to avoid a forward reference. */
  292.  
  293. private const char okiibm_init_string[]    = { 0x18 };
  294. private const char okiibm_end_string[]    = { 0x0c };
  295. private const char okiibm_one_direct[]    = { 0x1b, 0x55, 0x01 };
  296. private const char okiibm_two_direct[]    = { 0x1b, 0x55, 0x00 };
  297.  
  298. private int
  299. okiibm_print_page(gx_device_printer *pdev, FILE *prn_stream)
  300. {
  301.     char init_string[16], end_string[16];
  302.     int init_length, end_length;
  303.  
  304.     init_length = sizeof(okiibm_init_string);
  305.     memcpy(init_string, okiibm_init_string, init_length);
  306.  
  307.     end_length = sizeof(okiibm_end_string);
  308.     memcpy(end_string, okiibm_end_string, end_length);
  309.  
  310.     if ( pdev->y_pixels_per_inch > 72 &&
  311.          pdev->x_pixels_per_inch > 60 )
  312.     {
  313.         /* Unidirectional printing for the higher resolutions. */
  314.         memcpy( init_string + init_length, okiibm_one_direct,
  315.                 sizeof(okiibm_one_direct) );
  316.         init_length += sizeof(okiibm_one_direct);
  317.  
  318.         memcpy( end_string + end_length, okiibm_two_direct,
  319.                 sizeof(okiibm_two_direct) );
  320.         end_length += sizeof(okiibm_two_direct);
  321.     }
  322.     
  323.     return okiibm_print_page1( pdev, prn_stream, 
  324.                    pdev->y_pixels_per_inch > 72 ? 1 : 0,
  325.                    init_string, init_length,
  326.                    end_string, end_length );
  327. }
  328.